package com.resou.reader.utils.diskcache;

import android.os.AsyncTask;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.resou.reader.utils.ConfigManager;

import java.io.File;
import java.lang.reflect.Type;
import java.util.List;


/**
 * Created by Castiel on 2018/5/7.
 */

public class DiskCache {

    public static <T> void asyncWriteCache(final String key, final T data, final IWriteCacheListener listener) {
        if (TextUtils.isEmpty(key) || data == null) {
            return;
        }
        new AsyncTask<Void, Void, Boolean>() {
            @Override
            protected Boolean doInBackground(Void... params) {
                DiskCacheWrapper wrapper = DiskCacheWrapper.get(new File(ConfigManager.CACHE_FILE_DIR));
                try {
                    String json = (new Gson()).toJson(data);
                    wrapper.put(key, json);
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                    wrapper.closeCache();
                }
                return false;
            }

            @Override
            protected void onPostExecute(Boolean t) {
                super.onPostExecute(t);
                if (t) {
                    if (listener != null) {
                        listener.onWriteSuccess();
                    }
                } else {
                    if (listener != null) {
                        listener.onWriteFail();
                    }
                }
            }
        }.execute();
    }

    public static <T> boolean syncWriteCache(final String key, final T data) {
        DiskCacheWrapper wrapper = DiskCacheWrapper.get(new File(ConfigManager.CACHE_FILE_DIR));
        try {
            String json = (new Gson()).toJson(data);
            wrapper.put(key, json);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            wrapper.closeCache();
        }
        return false;
    }

    public static <T> T syncReadCache(final String key, final Class<?> clazz) {
        DiskCacheWrapper wrapper = DiskCacheWrapper.get(new File(ConfigManager.CACHE_FILE_DIR));
        try {
            String json = wrapper.get(key);
            if (!TextUtils.isEmpty(json)) {
                T o = (T) (new Gson()).fromJson(json, clazz);
                return o;
            }
        } catch (Exception e) {
            e.printStackTrace();
            wrapper.closeCache();
        }
        return null;
    }
//, final List<? super Object> T
    public static <T> List<T> syncReadListCache(final String key) {
        DiskCacheWrapper wrapper = DiskCacheWrapper.get(new File(ConfigManager.CACHE_FILE_DIR));
        try {
            String json = wrapper.get(key);
            if (!TextUtils.isEmpty(json)) {
                Type type = new TypeToken<List<T>>() {
                }.getType();
                return new Gson().fromJson(json, type);
            }
        } catch (Exception e) {
            e.printStackTrace();
            wrapper.closeCache();
        }
        return null;
    }

    public static <T> void asyncReadCache(final String key, final Class<?> clazz, final ILoadCacheListener listener) {
        new AsyncTask<Void, Void, T>() {
            @Override
            protected T doInBackground(Void... params) {
                DiskCacheWrapper wrapper = DiskCacheWrapper.get(new File(ConfigManager.CACHE_FILE_DIR));
                try {
                    String json = wrapper.get(key);
                    if (!TextUtils.isEmpty(json)) {
                        T o = (T) (new Gson()).fromJson(json, clazz);
                        return o;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    wrapper.closeCache();
                }
                return null;
            }

            @Override
            protected void onPostExecute(T t) {
                super.onPostExecute(t);
                if (t != null) {
                    if (listener != null) {
                        listener.onLoadSuccess(t);
                    }
                } else {
                    if (listener != null) {
                        listener.onLoadFail();
                    }
                }
            }
        }.execute();
    }

    public static void removeCache(final String key) {
        DiskCacheWrapper wrapper = DiskCacheWrapper.get(new File(ConfigManager.CACHE_FILE_DIR));
        try {
            wrapper.remove(key);
        } catch (Exception e) {
            e.printStackTrace();
            wrapper.closeCache();
        }
    }

    public interface ILoadCacheListener<T> {
        void onLoadSuccess(T t);

        void onLoadFail();
    }

    public interface IWriteCacheListener {
        void onWriteSuccess();

        void onWriteFail();
    }

}
